<?php

class GS_Score {
	const INTERFACE_URL = "http://db.178.com/wow/ss.php?lang=tw&id=";
	
    static public $GS_ItemTypes = array(
	    //"INVTYPE_RELIC" 
	    28=> array("SlotMOD" => 0.3164, "ItemSlot" => 18 ),
	    //"INVTYPE_TRINKET" 
	    12=> array("SlotMOD"=>0.5625,"ItemSlot"=>33 ),
	    //"INVTYPE_2HWEAPON" 
	    17=> array("SlotMOD"=>2.000,"ItemSlot"=>16 ),
	    //"INVTYPE_WEAPONMAINHAND" 
	    21=> array("SlotMOD"=>1.0000,"ItemSlot"=>16 ),
	    //"INVTYPE_WEAPONOFFHAND" 
	    22=> array("SlotMOD"=>1.0000,"ItemSlot"=>17 ),
	    //"INVTYPE_RANGED" 
	    26=> array("SlotMOD"=>0.3164,"ItemSlot"=>18 ),
	    /**add Bow*/
	    15=> array("SlotMOD"=>0.3164,"ItemSlot"=>18 ),
	    /**end add*/
	    //"INVTYPE_THROWN" 
	    25=> array("SlotMOD"=>0.3164,"ItemSlot"=>18 ),
	    //"INVTYPE_RANGEDRIGHT" 
	    27=> array("SlotMOD"=>0.3164,"ItemSlot"=>18 ),
	    //"INVTYPE_SHIELD" 
	    14=> array("SlotMOD"=>1.0000,"ItemSlot"=>17 ),
	    //"INVTYPE_WEAPON" 
	    13=> array("SlotMOD"=>1.0000,"ItemSlot"=>36 ),
	    //"INVTYPE_HOLDABLE" 
	    23=> array("SlotMOD"=>1.0000,"ItemSlot"=>17 ),
	    //"INVTYPE_HEAD" 
	    1=> array("SlotMOD"=>1.0000,"ItemSlot"=>1 ),
	    //"INVTYPE_NECK" 
	    2=> array("SlotMOD"=>0.5625,"ItemSlot"=>2 ),
	    //"INVTYPE_SHOULDER" 
	    3=> array("SlotMOD"=>0.7500,"ItemSlot"=>3 ),
	    //"INVTYPE_CHEST" 
	    5=> array("SlotMOD"=>1.0000,"ItemSlot"=>5 ),
	    //"INVTYPE_ROBE" 
	    20=> array("SlotMOD"=>1.0000,"ItemSlot"=>5 ),
	    //"INVTYPE_WAIST" 
	    6=> array("SlotMOD"=>0.7500,"ItemSlot"=>6 ),
	    //"INVTYPE_LEGS" 
	    7=> array("SlotMOD"=>1.0000,"ItemSlot"=>7 ),
	    //"INVTYPE_FEET" 
	    8=> array("SlotMOD"=>0.75,"ItemSlot"=>8),
	    //"INVTYPE_WRIST" 
	    9=> array("SlotMOD"=>0.5625,"ItemSlot"=>9 ),
	    //"INVTYPE_HAND" 
	    10=> array("SlotMOD"=>0.7500,"ItemSlot"=>10 ),
	    //"INVTYPE_FINGER" 
	    11=> array("SlotMOD"=>0.5625,"ItemSlot"=>31 ),
	    //"INVTYPE_CLOAK" 
	    16=> array("SlotMOD"=>0.5625,"ItemSlot"=>15 )
    );
     
    static public $GS_Rarity = array(
	    0=> array('Red' => 0.55, 'Green' => 0.55, 'Blue' => 0.55 ),
	    1=> array('Red' => 1.00, 'Green' => 1.00, 'Blue' => 1.00 ),
	    2=>array('Red' => 0.12, 'Green' => 1.00, 'Blue' => 0.00 ),
	    3=> array('Red' => 0.00, 'Green' => 0.50, 'Blue' => 1.00 ),
	    4=> array('Red' => 0.69, 'Green' => 0.28, 'Blue' => 0.97 ),
	    5=> array('Red' => 0.94, 'Green' => 0.09, 'Blue' => 0.00 ),
	    6=> array('Red' => 1.00, 'Green' => 0.00, 'Blue' => 0.00 ),
	    7=> array('Red' => 0.90, 'Green' => 0.80, 'Blue' => 0.50)
    );
    
    static public  $GS_Formula = array(
	    "A" => array(
	        4 => array("A"=> 91.4500, "B"=> 0.6500 ),
	        3 => array("A"=> 81.3750, "B"=> 0.8125 ),
	        2 => array("A"=> 73.0000, "B"=> 1.0000 )
	    ),
	    "B" => array(
	        4 => array("A"=> 26.0000, "B"=> 1.2000 ),
	        3 => array("A"=> 0.7500, "B"=> 1.8000 ),
	        2 => array("A"=> 8.0000, "B"=> 2.0000 ),
	        1 => array("A"=> 0.0000, "B"=> 2.2500 )
	        )
	 );
	  
     static public $GS_Quality = array(
		    7000 => array( 
		        "Red" => array( "A" => 0.99, "B" => 6000, "C" => 0.00000, "D" => 1 ), 
		        "Green" => array( "A" => 0.01, "B" => 6000, "C" => 0.00001, "D" => -1 ), 
		        "Blue" => array( "A" => 0, "B" => 0, "C" => 0, "D" => 0 ), 
		        "Description" => "Legendary" 
		    ), 
		    6000 => array(
		        "Red" => array( "A" => 0.94, "B" => 5000, "C" => 0.00006, "D" => 1 ),
		        "Green" => array( "A" => 0.47, "B" => 5000, "C" => 0.00047, "D" => -1 ),
		        "Blue" => array( "A" => 0, "B" => 0, "C" => 0, "D" => 0 ),
		        "Description" => "Legendary"
		    ),
		    5000 => array(
		        "Red" => array( "A" => 0.69, "B" => 4000, "C" => 0.00025, "D" => 1 ),
		        "Green" => array( "A" => 0.28, "B" => 4000, "C" => 0.00019, "D" => 1 ),
		        "Blue" => array( "A" => 0.97, "B" => 4000, "C" => 0.00096, "D" => -1 ),
		        "Description" => "Epic"
		    ),
		    4000 => array(
		        "Red" => array( "A" => 0.0, "B" => 3000, "C" => 0.00069, "D" => 1 ),
		        "Green" => array( "A" => 0.5, "B" => 3000, "C" => 0.00022, "D" => -1 ),
		        "Blue" => array( "A" => 1, "B" => 3000, "C" => 0.00003, "D" => -1 ),
		        "Description" => "Superior"
		    ),
		    3000 => array(
		        "Red" => array( "A" => 0.12, "B" => 2000, "C" => 0.00012, "D" => -1 ),
		        "Green" => array( "A" => 1, "B" => 2000, "C" => 0.00050, "D" => -1 ),
		        "Blue" => array( "A" => 0, "B" => 2000, "C" => 0.001, "D" => 1 ),
		        "Description" => "Uncommon"
		    ),
		    2000 => array(
		        "Red" => array( "A" => 1, "B" => 1000, "C" => 0.00088, "D" => -1 ),
		        "Green" => array( "A" => 1, "B" => 000, "C" => 0.00000, "D" => 0 ),
		        "Blue" => array( "A" => 1, "B" => 1000, "C" => 0.001, "D" => -1 ),
		        "Description" => "Common"
		    ),
		    1000 => array(
		        "Red" => array( "A" => 0.55, "B" => 0, "C" => 0.00045, "D" => 1 ),
		        "Green" => array( "A" => 0.55, "B" => 0, "C" => 0.00045, "D" => 1 ),
		        "Blue" => array( "A" => 0.55, "B" => 0, "C" => 0.00045, "D" => 1 ),
		        "Description" => "Trash"
		    )
	);
	
	public static function GearScore_GetPlayerScore($class, $items){
		$GearScore = 0; 
		$PVPScore = 0; 
		$ItemCount = 0; 
		$LevelTotal = 0; 
		$TitanGrip = 1; 
		$TempEquip = array(); 
		$TempPVPScore = 0;
		
		$itemsData = self::getItemData($items);
		if(!$itemsData) return 0;
		
		
		if(self::checkItemExists($items, 16) && self::checkItemExists($items, 17)){
			if($itemsData[16]['slot']==17){
				$TitanGrip = 0.5;
			}
		}
		
		if(self::checkItemExists($items, 17)){
			if($itemsData[17]['slot']==17){
				$TitanGrip = 0.5;
			}
			list($TempScore) = self::GearScore_GetItemScore($itemsData[17]['quality'], $itemsData[17]['level'], $itemsData[17]['slot']);
			if($class==3){ // 猎人
				$TempScore = $TempScore * 0.3164;
			}
			$GearScore = $GearScore + $TempScore * $TitanGrip;
		}
		
		for($i=1; $i<=18; $i++)
		{
			if($i!=4 and $i!=17){
				$item = $itemsData[$i];
				if($item)
				{
					list($TempScore) = self::GearScore_GetItemScore($item['quality'], $item['level'], $item['slot']);
					if($i==16 and $class==3) $TempScore *= 0.3164;
					if($i==18 and $class==3) $TempScore *= 5.3224;
					if($i==16) $TempScore = $TempScore * $TitanGrip;
					$GearScore = $GearScore + $TempScore;
					$ItemCount = $ItemCount + 1;
					$LevelTotal = $LevelTotal + $item['level'];
				}
			}
		}
		
		if($GearScore<0) return array(0, 0);
		if($ItemCount==0) return array(0, 0);
		return array('gs' => floor($GearScore), 'itemlevel' => floor($GearScore/$ItemCount));
		
	}
	
	public static function getItemData($items){
		$url = self::INTERFACE_URL . implode(",", $items);
		$content = http_request($url);
		$content = substr($content, strpos($content, 'var $gitems = ') + strlen('var $gitems = '), -1);
		$ret = array();
		$itemsData = json_decode($content);
		if($itemsData == NULL){
			return null;
		}
		foreach($items as $slotId=>$itemId){
			$obj = self::matchItem($itemsData, $itemId);
			if($items[$slotId]!=0 && $obj == null){
				return null;
			}
			$ret[$slotId] = $obj;
		}
		return $ret;
	}
	
	public static function matchItem($itemsData, $itemId)
	{
		foreach($itemsData as $item){
			if($item->id == $itemId){
				return self::object2array($item);
			}
		}
		return null;
	}
	
	public static function object2array($obj){
		if(!$obj) return null;
		$ret = array();
		foreach($obj as $k=>$v){
			$ret[$k]=$v;
		}
		return $ret;
	}
	
	public static function checkItemExists($items, $slot){
		if(isset($items[$slot]) && $items[$slot]!=0){
			return true;
		}else{
			return false;
		}
	}
	   
	static public function GearScore_GetQuality($ItemScore){
        $Red = 0.1; 
        $Blue = 0.1; 
        $Green = 0.1; 
        $GS_QualityDescription = "Legendary";
        if  (empty($ItemScore)) { 
        	return array(0, 0, 0, "Trash");
        }
	    for ($i = 0;$i<=6;$i++){
	        if ( $ItemScore > $i * 1000  &&  $ItemScore <= ( $i + 1 ) * 1000 ){ 
	            $Red = self::$GS_Quality[( $i + 1 ) * 1000]['Red']["A"] + 
	                  ((( $ItemScore - self::$GS_Quality[( $i + 1 ) * 1000]['Red']["B"])  *self::$GS_Quality[( $i + 1 ) * 1000]['Red']["C"])  *self::$GS_Quality[( $i + 1 ) * 1000]['Red']["D"]);
	            $Blue = self::$GS_Quality[( $i + 1 ) * 1000]['Green']["A"] + 
	                  ((( $ItemScore - self::$GS_Quality[( $i + 1 ) * 1000]['Green']["B"])*self::$GS_Quality[( $i + 1 ) * 1000]['Green']["C"])*self::$GS_Quality[( $i + 1 ) * 1000]['Green']["D"]);
	            $Green = self::$GS_Quality[( $i + 1 ) * 1000]['Blue']["A"] + 
	                  ((( $ItemScore - self::$GS_Quality[( $i + 1 ) * 1000]['Blue']["B"]) *self::$GS_Quality[( $i + 1 ) * 1000]['Blue']["C"]) *self::$GS_Quality[( $i + 1 ) * 1000]['Blue']["D"]);
	            //--if not ( Red ) or not ( Blue ) or not ( Green ) then return 0.1, 0.1, 0.1, nil; end
	            return array($Red, $Green, $Blue, self::$GS_Quality[( $i + 1 ) * 1000]['Description']);
	        }
		}
       return array(0.1, 0.1, 0.1, "Empty");
	}
	   
	static public function GearScore_GetItemScore($ItemRarity,$ItemLevel,$ItemEquipLoc){
//	    GearScore_ScoreBuff = GearScore_ScoreBuff or {}
//	    if GearScore_ScoreBuff[ItemLink] then return unpack(GearScore_ScoreBuff[ItemLink]) end
	    $QualityScale = 1; 
	    $PVPScale = 1; 
	    $PVPScore = 0; 
	    $GearScore = 0;
	    //if not ( ItemLink ) then return 0, 0; end
	    //local ItemName, ItemLink, ItemRarity, ItemLevel, ItemMinLevel, ItemType, ItemSubType, ItemStackCount, ItemEquipLoc, ItemTexture = GetItemInfo(ItemLink); local Table = {}; local Scale = 1.8618
	    $Scale = 1.8618;
	    $Table = array();
	    if ( $ItemRarity == 5 ){ 
	    	$QualityScale = 1.3; 
	    	$ItemRarity = 4;
	    }elseif ( $ItemRarity == 1 ){
	    	$QualityScale = 0.005;  
	    	$ItemRarity = 2;
	    }elseif ( $ItemRarity == 0 ){ 
	    	$QualityScale = 0.005;  
	    	$ItemRarity = 2;
	    }
	    if ( $ItemRarity == 7 ){
	    	$ItemRarity = 3; 
	    	$ItemLevel = 187.05; 
	    }
	    
	    if ( self::$GS_ItemTypes[$ItemEquipLoc] ) {
	    	$Table = self::$GS_Formula["B"];
	        if ( $ItemLevel > 120 ){
	        	$Table = self::$GS_Formula["A"]; 
	        }
	        if ( $ItemRarity >= 2 &&  $ItemRarity <= 4 ){
	            $color = self::GearScore_GetQuality((floor((($ItemLevel - $Table[$ItemRarity]['A']) / $Table[$ItemRarity]['B']) * 1 * $Scale)) * 11.25 );
	            $GearScore = floor((($ItemLevel - $Table[$ItemRarity]['A']) / $Table[$ItemRarity]['B']) * self::$GS_ItemTypes[$ItemEquipLoc]['SlotMOD'] * $Scale * $QualityScale);
	            if ( $ItemLevel == 187.05 ) {
	            	$ItemLevel = 0;
	            } 
	            if ( $GearScore < 0 ){
	            	$GearScore = 0;
	            	$color = self::GearScore_GetQuality(1);
	            } 
	            if ( $PVPScale == 0.75 ){
	            	$PVPScore = 1; 
	            	$GearScore = $GearScore * 1; 
	            }else {
	            	$PVPScore = $GearScore * 0;
	            }
	            $GearScore = floor($GearScore);
	            $PVPScore = floor($PVPScore);
	            //GearScore_ScoreBuff[ItemLink] = {GearScore, ItemLevel, GS_ItemTypes[ItemEquipLoc].ItemSlot, Red, Green, Blue, PVPScore, ItemEquipLoc}
	            return array($GearScore,$color);
	       }
        }
	    //GearScore_ScoreBuff[ItemLink] = {-1, ItemLevel, 50, 1, 1, 1, PVPScore, ItemEquipLoc}
	    return array(-1, $color);
	}
	
	static public function GearScrore_GetHunterItemScore($ItemRarity,$ItemLevel,$ItemEquipLoc){
//         if hunter  , melee weapon GS*0.3164, ranged weapon GS*5.3224
//         no implement , because it relates to which weapon hunter cound equip. (complex,no time)
	}
}
/*

include_once(realpath(dirname(__FILE__))."/../bootstrap.php");
$items = Array ( 1 => 37414 ,2 => 38226 ,3 => 42950 ,4 => 23345 ,5 => 48677 ,6 => 37421 ,7 => 36886 ,8 => 42766 ,9 => 37458 ,10 => 42763 ,11 => 38251 ,12 => 38218 ,15 => 36998 ,16 => 42944 ,17 => 42944 ,0 => 24417 ) ;
list($gs) = GS_Score::GearScore_GetPlayerScore(3, $items);

echo $gs;
*/